package com.liming.admin.controller;

import com.liming.common.result.LMResult;
import com.liming.file.client.AliClient;
import com.liming.file.client.LocalFileClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author liming
 * @date 2025/2/15
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "文件接口-Web")
@RequestMapping("/admin/file")
public class FileController {

    private final AliClient aliClient;
    private final LocalFileClient localFileClient;

    @Operation(summary = "根据文件名获取本地文件")
    @GetMapping("{fileName}")
    public void getFileByName(@PathVariable String fileName) {
        try {
            localFileClient.getFileByName(fileName);
        } catch (Exception e) {
            log.error("获取文件失败", e);
        }
    }

    @Operation(summary = "上传文件")
    @PostMapping("upload")
    private LMResult<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            String url = aliClient.uploadOss(file);
            return LMResult.success(url);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return LMResult.error("上传文件失败");
        }
    }
}
